using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Graphics;
using GameLib.GameItem;

namespace GameLib.GameScreen
{
    class GameTitleScreen : GameScreen
    {
        Texture2D background;
        Texture2D banner;
        ItemManager itemManager;

        public GameTitleScreen(ScreenManager manager)
            : base(manager)
        {
            Load();
        }

        public override void Load()
        {
            background = content.Load<Texture2D>(@"Images\background");
            banner = content.Load<Texture2D>(@"Images\banner");

            itemManager = new ItemManager();

            GameButtonR btn_Sound = new GameButtonR(content.Load<Texture2D>(@"Images\speaker"), new Vector2(5, 430), new Point(45, 45), SoundManager.IsSoundOn() ? 1 : 0, itemManager);
            btn_Sound.ItemPress += new EventHandler(btn_Sound_Press);
            itemManager.AddItem(btn_Sound);

            GameButton btn_Start = new GameButton(content.Load<Texture2D>(@"Images\btn_Start"), new Vector2(285, 285), null, null, itemManager);
            btn_Start.ItemPress += new EventHandler(btn_Start_Press);
            itemManager.AddItem(btn_Start);

            GameButton btn_Exit = new GameButton(content.Load<Texture2D>(@"Images\btn_Exit"), new Vector2(319, 375), null, null, itemManager);
            btn_Exit.ItemPress += new EventHandler(btn_Exit_Press);
            itemManager.AddItem(btn_Exit);

            this.LoadComplete = true;
        }

        public override void UnLoad()
        {
            itemManager.Clear();
            base.UnLoad();
        }

        public override void Update(GameTime gametime)
        {
            TouchCollection touchCollection = TouchPanel.GetState();
            foreach (TouchLocation touch in touchCollection)
            {
                if ((touch.State == TouchLocationState.Pressed))
                {
                    itemManager.CheckPress(touch.Position);
                }
            }

            itemManager.Update(gametime);
            base.Update(gametime);
        }

        public override void Draw(SpriteBatch spritebatch)
        {
            spritebatch.Draw(background, Vector2.Zero, Color.White);
            spritebatch.Draw(banner, Vector2.Zero, Color.White);
            itemManager.Draw(spritebatch);

            base.Draw(spritebatch);
        }

        private void btn_Sound_Press(object sender, EventArgs e)
        {
            (sender as GameButtonR).NextFrame();
            if (SoundManager.IsSoundOn())
            {
                SoundManager.SetSoundOff();
            }
            else
            {
                SoundManager.SetSoundOn();
            }
        }

        private void btn_Start_Press(object sender, EventArgs e)
        {
            manager.RemoveAndAdd(this, ScreenName.GameLevel);
        }

        private void btn_Exit_Press(object sender, EventArgs e)
        {
            manager.Exit();
        }
    }
}
